package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.client.utils.DateUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author shaki
 * @since 2021-05-24
 */
@Api("讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Resource
    private EduTeacherService eduTeacherService;
    // 查询所有
    @ApiOperation("所有讲师列表")
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }
    //逻辑删除
    @ApiOperation("根据id逻辑删除讲师")
    @DeleteMapping("deleteById/{id}")
    public R deleteById(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String id){
        boolean remove = eduTeacherService.removeById(id);
        if(remove){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //分页查询
    @GetMapping("pageTeacher/{current}/{size}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long size){
        Page<EduTeacher> pageTeacher = new Page<>(current,size);
        //实现分页
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        Map map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    // 条件查询带分页功能
    // GetMapping 不支持@RequestBody
    @PostMapping("pageListTeacherCondition/{current}/{size}")
    public R pageListTeacherCondition(@PathVariable long current,
                                      @PathVariable long size,
                                      @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> pageTeacher = new Page<>(current,size);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空就拼接条件
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isNullOrEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(level!= null) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isNullOrEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isNullOrEmpty(end)) {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher){
        boolean save = eduTeacherService.save(teacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //修改讲师
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher teacher){
        boolean update = eduTeacherService.updateById(teacher);
        if(update){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据讲师id查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }
}

